//
//  YKViewController.m
//  WeChat
//
//  Created by 吴高烽 on 16/5/20.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD+HM.h"
#import "YKXMPPTool.h"
@interface YKViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userTextF;
@property (weak, nonatomic) IBOutlet UITextField *pswTextF;

@end

@implementation YKViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
//点击登陆调用的方法
- (IBAction)login:(UIButton *)sender {
//   1.判断有没有输入用户名和密码
    if (self.userTextF.text.length == 0 || self.pswTextF.text.length == 0) {
        YKLog(@"请输入用户名和密码");
        return ;
    }
    [MBProgressHUD showMessage:@"正在登陆中···"];
//    2. 登录服务器
//    2.1保存用户名和密码到沙盒
    YKAccount *account  =[YKAccount shareAccount];
    account.loginUser = self.userTextF.text ;
    account.loginPwd = self.pswTextF.text ;
    [account saveToSandBox];
//[YKXMPPTool shareYKXMPPTool]
    __weak typeof(self) weakSelf = self;
    [[YKXMPPTool shareYKXMPPTool] xmppLogin:^(XMPPResultTyep resultType) {
        [weakSelf handleXMPPResultType:resultType];
    }];
//    将登陆的结果告诉改控制器
    
    
}
-(void)dealloc{
  YKLog(@"%s",__func__);
}
-(void)handleXMPPResultType:(XMPPResultTyep)resultType{
dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUD];
    if (resultType ==XMPPResultTyepLoginSuccess) {
        YKLog(@"登录成功");
        [UIStoryboard showInitialVCWithName:@"Main"];
        [YKAccount shareAccount].login = YES;
        [[YKAccount shareAccount] saveToSandBox];
    }else{
        YKLog(@"登录失败");
        [MBProgressHUD showError:@"用户名或密码错误"];
    }
});
    

}
//-(void)changeToMain{
////    将切换操作放在主线程中进行，更新UI线程的操作放在主线程中
//    dispatch_async(dispatch_get_main_queue(), ^{
//    // 获取main.storyboard 的第一个控制器
//    id vc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateInitialViewController];
//    //    切换window的根控制器
//    [UIApplication sharedApplication].keyWindow.rootViewController = vc ;
//    });
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
